package com.minyifang.wcspf.work.ui.user.presenter;

import com.minyifang.wcspf.common.base.BasePresenter;
import com.minyifang.wcspf.common.config.network.Constants;
import com.minyifang.wcspf.common.config.network.RetrofitHelper;
import com.minyifang.wcspf.common.config.user.UserManager;
import com.minyifang.wcspf.work.ui.user.view.AddressView;
import com.minyifang.wcspf.work.ui.user.view.MyWalletView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class AddressPresenter extends BasePresenter<AddressView> {

    public void getAddressList() {
        getView().showLoadingView();
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), Constants.getJsonData(null, UserManager.getInstance().getToken()).toString());
        addTask(RetrofitHelper.getInstance().getService().getAddressList(requestBody), respond -> {
            getView().hideLoadingView();
            getView().showAddressList(respond);
        });
    }

    public void delAddress(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("addressId", id);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().delAddress(requestBody), respond -> getView().showDelRes(respond));
    }

    public void setDefAddress(String id) {
        Map<String, Object> map = new HashMap<>();
        map.put("addressId", id);
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().setDefAddress(requestBody), respond -> getView().showSetDefRes(respond));
    }
}
